﻿using blackhouse.mojoPortalExtend;
using blackhouse.NemoBlog.Business;
using blackhouse.NemoBlog.Business.View;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.NemoBlog.Web {
    public partial class StandardNemoBlogPost : UserControl, IPostView {

        #region Fields

        public string shortDescription;
        private PageProvider pageProvider = new PageProvider();

        #endregion

        #region Properties

        public int PostId {
            get;
            set;
        }

        public string PostTitle {
            get {
                return this.ltlPost.Text;
            }
            set {
                this.pageProvider.SetPageTitle(value);
                this.ltlPost.Text = value;
            }
        }

        public string ShortTitle {
            get;
            set;
        }

        public string ShortDescription {
            get { return this.shortDescription; }
            set {
                if (value != this.shortDescription) {
                    this.shortDescription = value;
                    this.pageProvider.SetPageDescription(this.shortDescription);
                }
            }
        }

        public string PostContent {
            get { return this.ltlPostContent.Text; }
            set { this.ltlPostContent.Text = value; }
        }

        public string PublishedDateText {
            get { return this.ltlPublishDate.Text; }
            set { this.ltlPublishDate.Text = value; }
        }

        public string IsoPublishedDateText {
            get { return this.ltlIsoPublishDate.Text; }
            set { this.ltlIsoPublishDate.Text = value; }
        }

        public string Publisher {
            get { return this.ltlPublisher.Text; }
            set { this.ltlPublisher.Text = value; }
        }

        public bool SimilarPostsVisible {
            get { return this.pnlSimilarPosts.Visible; }
            set { this.pnlSimilarPosts.Visible = value; }
        }

        #endregion

        public void PopulateTags(NemoBlogTag[] tags) {
            if (tags.Length > 0) {
                this.rptTags.Visible = true;
                this.rptTags.DataSource = tags.Select(t => new DTNemoBlogTag(t));
                this.rptTags.DataBind();
            }
            else {
                this.rptTags.Visible = false;
            }
        }

        public void PopulateSimilarPosts(NemoBlogPost[] similarPosts) {
            this.rptSimilarPosts.DataSource = similarPosts.Select(p => new DTNemoBlogPost(p));
            this.rptSimilarPosts.DataBind();
        }

        public void SetValidUrl(string postUrl) {
            string fPostUrl = postUrl.ReplaceEx("~/", "/");
            if (!this.Request.RawUrl.EndsWith(fPostUrl)) {
                this.Response.RedirectPermanent(fPostUrl);
                return;
            }
            this.pageProvider.SetCanonicalUrl(fPostUrl);
        }

        public void SetRemoveLink(int postId) {
            this.pnlActionPanel.Visible = true;
            this.phRemoveLink.Visible = true;
        }

        public void SetPublishLink(int postId) {
            this.pnlActionPanel.Visible = true;
            this.phPublishLink.Visible = true;
        }

        public void SetEditLink(Business.NemoBlogPost post) {
            this.pnlActionPanel.Visible = true;
            this.phEditLink.Visible = true;
            this.lteEditPost.GroupSysName = post.GetGroup().SystemName;
            this.lteEditPost.RecordId = post.PostId;
            this.lteEditPost.AddSpecialParameter("pageId", CacheHelper.GetCurrentPage().PageId.ToString());
            this.lteEditPost.AddSpecialParameter("mid", post.ModuleId.ToString());
        }

        public void ShowPostLists(int placeId) {
            Module m = new Module(placeId);
            if (m.ModuleId == placeId) {
                PageSettings p = new PageSettings(m.SiteId, m.PageId);
                Response.Redirect(p.Url.ReplaceEx("~/", "/"));
            }
        }

        public void Refresh() {
            Response.Redirect(Request.RawUrl);
        }

        public event EventHandler Remove {
            add {
                this.lnkRemove.Click += value;
            }
            remove {
                this.lnkRemove.Click -= value;
            }
        }

        public event EventHandler Publish {
            add {
                this.lnkPublish.Click += value;
            }
            remove {
                this.lnkPublish.Click -= value;
            }
        }

    }
}